package com.rin.service;

import com.alibaba.fastjson.JSON;
import com.rin.constant.OperationEnum;
import com.rin.executor.AnnotationExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;

/**
 * @author RinMi
 * @since 2023/6/13
 */

@Service
public class InsertService extends BaseService {

    @Resource
    private AnnotationExecutor annotationExecutor;

    public void insert(Object entity) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        String entityId = annotationExecutor.insertExecutor(entity);
        //获取json
        String entityJson = JSON.toJSONString(entity);
        //将字符串转化为字节流
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(entityJson.getBytes(StandardCharsets.UTF_8));
        // 调用create方法指定文件上传，参数HDFS上传路径
        String destination = createDestination(entity.getClass(), entityId);
        //上传
        updateIdList(entity, entityId, OperationEnum.INSERT.ordinal());
        upload(byteArrayInputStream, destination);
    }

}
